/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package it.kirys.rilego.gui.customclasses;

import it.kirys.rilego.engine.loaders.imagesources.IImageSource;
import it.kirys.rilego.engine.loaders.imagesources.SourceRotation;
import it.kirys.rilego.engine.project.Project;
import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author Federico Improta
 */
public class SourcesTableModel extends AbstractTableModel {

	public enum Column {

		Rotate(0, "Rotate", "Rotate"),
		Split(1, "Split", "Split"),
		Orientation(2, "Orientation", "Orientation"),
		Name(3, "Name", "Name"),
		Info(4, "Info", "Info");
		int column;
		String hint;
		String label;

		public String getHint() {
			return hint;
		}

		public int getColumn() {
			return column;
		}

		public static Column translate(int col) {
			for (Column c : Column.values()) {
				if (c.column == col) {
					return c;
				}
			}
			return null;
		}//public Column translate(int col)

		public String getLabel() {
			return label;
		}

		private Column(int column, String label, String hint) {
			this.column = column;
			this.label = label;
			this.hint = hint;
		}
	}
	private Project project;

	public SourcesTableModel(Project project) {
		this.project = project;
	}

	public void assignToTable(JTable table) {
		TableColumnModel columnModel = table.getColumnModel();
		table.setModel(this);

		//set a combo box for the image rotation
		TableColumn rotateColumn = columnModel.getColumn(Column.Rotate.getColumn());
		JComboBox rotateEditor = new JComboBox(SourceRotation.values());
		rotateColumn.setCellEditor(new DefaultCellEditor(rotateEditor));
		initColumnSizes(table);
	}//public void assignToTable(JTable table)

	private void initColumnSizes(JTable table) {
		TableColumn column = null;
		Component comp = null;
		int headerWidth = 0;
		int cellWidth = 0;
		TableCellRenderer headerRenderer =
				  table.getTableHeader().getDefaultRenderer();

		for (Column c : Column.values()) {
			int i = c.getColumn();
			column = table.getColumnModel().getColumn(i);

			comp = headerRenderer.getTableCellRendererComponent(
					  null, column.getHeaderValue(),
					  false, false, 0, 0);
			headerWidth = comp.getPreferredSize().width;

			cellWidth = this.getPreferredSize(i);
			column.setPreferredWidth(this.getPreferredSize(i));
			column.setMaxWidth(this.getMaxSize(i));
			column.setMinWidth(this.getMinSize(i));
		}
	}

	@Override
	public int getRowCount() {
		if (project != null) {
			return project.getSources().size();
		} else {
			return 0;
		}

	}//public int getRowCount()

	@Override
	public int getColumnCount() {
		return Column.values().length;
	}

	@Override
	public String getColumnName(int col) {
		Column c = Column.translate(col);
		if (c != null) {
			return c.getLabel();
		} else {
			return "";
		}
	}

	public int getPreferredSize(int col) {
		Column c = Column.translate(col);
		switch (c) {
			case Rotate:
				return 60;
			case Name:
				return 150;
			case Orientation:
				return 20;
			case Split:
				return 20;
			case Info:
				return 40;
			default:
				return 40;
		}
	}

	public int getMaxSize(int col) {
		Column c = Column.translate(col);
		switch (c) {
			case Rotate:
				return 100;
			case Name:
				return 3000;
			case Orientation:
				return 20;
			case Split:
				return 20;
			case Info:
				return 100;
			default:
				return 40;
		}
	}

	public int getMinSize(int col) {
		Column c = Column.translate(col);
		switch (c) {
			case Rotate:
				return 40;
			case Name:
				return 100;
			case Orientation:
				return 20;
			case Split:
				return 20;
			case Info:
				return 40;
			default:
				return 40;
		}
	}

	static public String getColumnHint(int col) {
		Column c = Column.translate(col);
		if (c != null) {
			return c.getHint();
		} else {
			return "";
		}

	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int col) {
		Column c = Column.translate(col);
		IImageSource item = project.getSources().get(rowIndex);
		if (item != null) {
			switch (c) {
				case Split:
					if (aValue instanceof Boolean) {
						item.getTransformations().setSplit(((Boolean) aValue).booleanValue());
					}
				case Rotate:
					if (aValue instanceof SourceRotation) {
						item.getTransformations().setRotate(((SourceRotation) aValue));
					}

				default:
			}
		}
		fireTableRowsUpdated(rowIndex, rowIndex);
	}//public void setValueAt(Object aValue, int rowIndex, int col)

	@Override
	public boolean isCellEditable(int row, int col) {
		Column c = Column.translate(col);
		switch (c) {
			case Split:
			case Rotate:
				return true;
			default:
				return false;
		}
	}

	@Override
	public Class getColumnClass(int c) {
		if (project.getSources().size() > 0) {
			return getValueAt(0, c).getClass();
		} else {
			return (new String()).getClass();
		}
	}

	public String getCellHint(int rowIndex, int columnIndex) {
		Column c = Column.translate(columnIndex);
		IImageSource item = project.getSources().get(rowIndex);
		if (item != null) {
			switch (c) {
				case Name:
					return item.getFullIdentifier();
				default:
					return null;
			}
		} else {
			return null;
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Column c = Column.translate(columnIndex);
		IImageSource item = project.getSources().get(rowIndex);
		if (item != null) {
			switch (c) {
				case Rotate:
					if (item.getTransformations() != null) {
						return item.getTransformations().getRotate();
					} else {
						return null;
					}
				case Split:
					if (item.getTransformations() != null) {
						return Boolean.valueOf(item.getTransformations().isSplit());
					} else {
						return null;
					}
				case Orientation:
					if (item.getInfo().isKnown()) {
						return item.getInfo().getOrientation().getLabel().substring(0, 1);
					} else {
						return "?";
					}
				case Name:
					return item.getIdentifier();

				case Info: {
					if (item.getInfo().isKnown()) {
						return String.format("%d/%d", item.getInfo().getWidth(), item.getInfo().getHeight());
					} else {
						return "?";
					}
				}

				default:
					return "N.A.";
			}

		} else {
			return "";
		}
	}
}
